snowsoft.gibay._LOG.info("Loading class, 'snowsoft.gibay.Home'");

/**
 * Calls GetCategories data services
 * Handles Home Tab UI updates
 * @author digitalghost.liu@gmail.com 
 */
jsx3.lang.Class.defineClass("snowsoft.gibay.Home",null,[],function(Home){

	//shortcut to package
	var gibay = snowsoft.gibay;	

	
	//private method : if a category isn't a leaf category(it doesn't contains sub category),give it a icon .
	var _populateCategoryImages = function(doc){
		var objRecs = doc.selectNodes("//record");
    		for(var i=0;i<objRecs.getLength();i++) {
      			var objRec = objRecs.getItem(i);
			if(objRec.getAttribute("LeafCategory") == "false"){
				objRec.setAttribute("jsximg","images/folder.png");
			}
      	}
		
	};
	
	//private method : make selected categories in category list unselectable
	var _checkSelectedCategories = function(doc){
		var objBlkSearchList = gibay.APP.getJSXByName("blkSearchList");
		var objMtxCategories = gibay.APP.getJSXByName("mtxCategories");
		var children = objBlkSearchList.findDescendants(function(o) { return o.recordProperties; },true,true,false,false);
		for(var i=0;i<children.length;i++){
			var category = children[i];
			var record = category.recordProperties;
			if(doc.getRecord(record.jsxid)!=null){
				//TO DO: disable the row, since it can't be added a second time?
    				objMtxCategories.insertRecordProperty(record.jsxid,"jsxunselectable","1",false);
    				objMtxCategories.deselectRecord(record.jsxid);
    				objMtxCategories.insertRecordProperty(record.jsxid,"jsxcellstyle","color:#e6e6e6;",true);	
			}
		}


	};

	  
	//Calls Web Service to get the categories of ebay us site
	Home.getCategories = function(objEvent){
		try{
			gibay.APP.getJSXByName("blkCategories").showMask("Loading...");
			var objService = gibay.APP.loadResource("trading_xml");
			objService.setOperation("GetCategories");
			objService.subscribe(jsx3.net.Service.ON_SUCCESS,Home.onGetCategroiesSuccess);	
			objService.subscribe(jsx3.net.Service.ON_ERROR,Home.onGetCategroiesError);
			objService.startTime = (new Date()).valueOf();	
			objService.categoryParentID = objEvent.categoryID;
			objService.categoryLevel = objEvent.categoryLevel+1;
			gibay._LOG.info("Call getCategories service....");
			objService.doCall();
		}catch(e){
			var objError = jsx3.lang.NativeError.wrap(e);
			gibay._LOG.error(objError.getMessage());	
		}
	};

	Home.onGetCategroiesSuccess = function(objEvent){

		var tTotal = ((new Date()).valueOf() - objEvent.target.startTime) / 1000;
		gibay._LOG.info("Call getCategories service successful.."+tTotal+" secs");

		var objDoc = gibay.APP.getCache().getDocument("MODEL_Categories");
		_populateCategoryImages(objDoc);
		_checkSelectedCategories(objDoc);
	
		//remove parent category from document
		var docNav = gibay.ModelManager.getNavPath();
		var objRecs = docNav.selectNodes("//record");
		var recID = objRecs.getItem(objRecs.getLength()-1).getAttribute("jsxid");//current category id
		var objRecs = objDoc.selectNodes("//record");
    		for(var i=0;i<objRecs.getLength();i++) {
      			var objRec = objRecs.getItem(i);
				if(objRec.getAttribute("jsxid") == recID){
					objDoc.deleteRecord(objRec.getAttribute("jsxid"));	
					continue;
				}
      		}
		
		gibay.APP.getJSXByName("blkCategories").hideMask();
		gibay.APP.getJSXByName("mtxCategories").repaintData();
	};
	
	Home.onGetCategroiesError = function(objEvent){
		var tTotal = ((new Date()).valueOf() - objEvent.target.StartTime) / 1000;
    	giaby._LOG.info("Get service error, 'Response':\n__________________\n" + objEvent.target.getInboundDocument() + "\n__________________\n response time: " + tTotal + " seconds.");
    	gibay.APP.getJSXByName("blkCategories").hideMask();
	};
	
	//method for refresh home to display the MODEL_* status
	Home.refreshHome = function(objEvent){
		gibay._LOG.info("Refresh home tab for MODEL_* changes.");
		gibay.APP.getJSXByName("blkxRecentItems").repaint();
		gibay.APP.getJSXByName("blkxWatchList").repaint();
		gibay.APP.getJSXByName("blkxPopularKeywords").repaint();
	};		
});


//subscribe handler method to relevant events
snowsoft.gibay.APP.subscribe(snowsoft.gibay.Event.ON_CATEGORY_CHANGES,snowsoft.gibay.Home.getCategories);
snowsoft.gibay.APP.subscribe(snowsoft.gibay.Event.ON_REFRESHHOME,snowsoft.gibay.Home.refreshHome);
snowsoft.gibay.APP.subscribe(snowsoft.gibay.Event.ON_MODELPOPULARKEYWORDSUPDATED,snowsoft.gibay.Home.refreshHome);

